using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Specialized;
using System.Text;

namespace ClickSocial
{
    public class Paginador
    {
        private PagedDataSource m_pageds;

        public PagedDataSource PageDS
        {
            get { return m_pageds; }
        }

        protected string m_page_index;
        protected string m_page_name;
        protected string m_querys;

        public Paginador(string pageIndex, string pageName, NameValueCollection queryString)
        {
            m_pageds = new PagedDataSource();
            m_page_index = pageIndex;
            m_page_name = pageName;
            m_querys = string.Empty;

            if (queryString.HasKeys())
            {
                IEnumerator myEnumerator = queryString.GetEnumerator();                
                StringBuilder sbQuerys = new StringBuilder();
                bool primero = true;

                foreach (String key in queryString.AllKeys)
                {
                    if (key != "page")
                    {
                        if (primero)
                            primero = false;
                        else
                            sbQuerys.Append("&");

                        sbQuerys.AppendFormat("{0}={1}", key, queryString[key]);
                    }
                }
                m_querys = sbQuerys.ToString();
            }


        }

        public string Render()
        {
            StringBuilder sbHtml = new StringBuilder();
            int intPage = 0;

            if (!string.IsNullOrEmpty(m_page_index))
            {
                intPage = int.Parse(m_page_index) - 1;

                if (intPage < 0)
                {
                    intPage = 0;
                }

                if (intPage > m_pageds.PageCount)
                {
                    intPage = m_pageds.PageCount - 1;
                }
                m_pageds.CurrentPageIndex = intPage;
            }

            if (m_pageds.CurrentPageIndex != 0)
            {
                if(m_querys==string.Empty)
                    sbHtml.AppendFormat("<a href='{0}' class='PagerIni'", m_page_name);
                else
                    sbHtml.AppendFormat("<a href='{0}?{1}' class='PagerIni'", m_page_name, m_querys);
                sbHtml.Append(">");
                sbHtml.Append(" << primera ");// inicio
                sbHtml.Append("</a>");

                if (m_querys == string.Empty)
                    sbHtml.AppendFormat("<a href='{0}?page={1}' class='PagerAtras'", m_page_name, m_pageds.CurrentPageIndex);
                else
                    sbHtml.AppendFormat("<a href='{0}?page={1}&{2}' class='PagerAtras'", m_page_name, m_pageds.CurrentPageIndex, m_querys);
                sbHtml.Append(">");
                sbHtml.Append(" < anterior");// atras
                sbHtml.Append("</a>");
            }

            sbHtml.Append(CreatePagerLinks(m_pageds, m_page_name));

            if (!m_pageds.IsLastPage)
            {
                if (m_querys == string.Empty)
                    sbHtml.AppendFormat("<a href='{0}?page={1}' class='PagerSig'", m_page_name, (m_pageds.CurrentPageIndex + 2));
                else
                    sbHtml.AppendFormat("<a href='{0}?page={1}&{2}' class='PagerSig'", m_page_name, (m_pageds.CurrentPageIndex + 2), m_querys);
                sbHtml.Append(">");
                sbHtml.Append(" siguiente >");// siguiente
                sbHtml.Append("</a>");

                if (m_querys == string.Empty)
                    sbHtml.AppendFormat("<a href='{0}?page={1}' class='PagerUlt'", m_page_name, m_pageds.PageCount);
                else
                    sbHtml.AppendFormat("<a href='{0}?page={1}&{2}' class='PagerUlt'", m_page_name, m_pageds.PageCount, m_querys);
                sbHtml.Append(">");
                sbHtml.Append(" ultima >>");// ultimo
                sbHtml.Append("</a>");
            }

            return sbHtml.ToString();

        }

        protected string CreatePagerLinks(PagedDataSource pageds, string BaseUrl)
        {
            System.Text.StringBuilder sbpager = new System.Text.StringBuilder();
            int intLow = pageds.CurrentPageIndex - 1;
            int intHigh = pageds.CurrentPageIndex;


            if (pageds.PageCount==1) return string.Empty;

            if ((intLow < 1))
            {
                intLow = 1;
            }
            if (intHigh > 1)
            {
                intHigh = 1;
            }
            if ((intHigh > pageds.PageCount))
            {
                intHigh = pageds.PageCount;
            }
            if ((intHigh - intLow < 10))
            {
                while (((intHigh < intLow + 9) & intHigh < pageds.PageCount))
                {
                    intHigh = intHigh + 1;
                }
            }
            if ((intHigh - intLow < 10))
            {
                while (((intLow > intHigh - 9) & intLow > 1))
                {
                    intLow = intLow - 1;
                }
            }
            int x = 0;
            for (x = intLow; x <= intHigh; x++)
            {
                if ((x == pageds.CurrentPageIndex + 1))
                {
                    sbpager.Append(x.ToString() + "  ");
                }
                else
                {
                    if (m_querys == string.Empty)
                        sbpager.AppendFormat("<a href='{0}?page={1}' class='PagerNumeric'> {2} </a>", BaseUrl, x, x);
                    else
                        sbpager.AppendFormat("<a href='{0}?page={1}&{2}' class='PagerNumeric'> {3} </a>", BaseUrl, x, m_querys, x);
                }
            }

            return sbpager.ToString();
        }
    }
}
